import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gold/app/user_provider.dart';
import 'package:gold/base/config.dart';
import 'package:gold/model/shop_model.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/utils/xfg_utils.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/http_image.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:provider/provider.dart';

/// @description:
/// @author tangzhentao
/// @created at 21:38 2024/11/25

class MyShopPage extends StatelessWidget {

  const MyShopPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBody: true,
        body: Container(
          decoration: const BoxDecoration(image: DecorationImage(image: AssetImage(Res.imagesHomeTop), fit: BoxFit.fitWidth, alignment: Alignment.topCenter)),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                xfgAppbar(title: '我的店铺', backgroundColor: XfgColor.transparent, systemOverlayStyle: SystemUiOverlayStyle.light),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                  child: Column(
                    children: [
                      /// 店主信息
                      Builder(builder: (ctx) {
                        final shop = ctx.select<UserProvider, ShopModel>((k) => k.shop);
                        return Container(
                          margin: EdgeInsets.only(top: XfgSize().size12),
                          height: XfgSize().size70,
                          child: Row(
                            children: [
                              HttpImage(shop.user?.avatar, size: Size.square(XfgSize().size70), placeholder: Res.imagesAvatar, radius: BorderRadius.circular(XfgSize().size70)),
                              SizedBox(width: XfgSize().size18),
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Row(
                                      children: [
                                        XfgText(shop.user?.nickname?? '梦想家', fontSize: XfgSize().text20, color: XfgColor.white, bold: FontWeight.w500),
                                        Container(
                                          margin: EdgeInsets.only(left: XfgSize().size3),
                                          height: XfgSize().size16,
                                          width: XfgSize().size14 + xfgBoundingTextSize('V0会员', style: TextStyle(fontSize: XfgSize().text10)).width,
                                          decoration: BoxDecoration(border: Border.all(color: XfgColor.white, width: XfgSize().size0_5), borderRadius: BorderRadius.circular(XfgSize().size16)),
                                          alignment: Alignment.center,
                                          child: XfgText('V0会员', fontSize: XfgSize().text10, color: XfgColor.white),
                                        )
                                      ],
                                    ),
                                    SizedBox(height: XfgSize().size12),
                                    XfgText('UID:${shop.user?.uid?? ''}', fontSize: XfgSize().text16, color: XfgColor.white),
                                  ],
                                ),
                              ),
                              GestureDetector(
                                child: Container(
                                  width: XfgSize().size100,
                                  height: XfgSize().size31,
                                  decoration: BoxDecoration(gradient: XfgColor.withdrawalGradient, borderRadius: BorderRadius.circular(XfgSize().size31)),
                                  alignment: Alignment.center,
                                  child: XfgText('推广店铺', fontSize: XfgSize().text14, color: XfgColor.theme),
                                ),
                              )
                            ],
                          ),
                        );
                      }),
                      /// 资金
                      Container(
                        decoration: white6Decoration,
                        margin: EdgeInsets.only(top: XfgSize().size20),
                        child: Builder(builder: (ctx) {
                          final shop = ctx.select<UserProvider, ShopModel>((k) => k.shop);
                          return Column(
                            children: [
                              SizedBox(
                                height: XfgSize().size80,
                                child: Row(
                                  children: [
                                    _shopInfoItemWidget('总额', shop.totalAmount?.toStringAsFixed(1)?? '0.0', unit: '¥'),
                                    _shopInfoItemWidget('待收货款', shop.unpaidAmount?.toStringAsFixed(2)?? '0.00', unit: '¥'),
                                    _shopInfoItemWidget('已收货款', shop.paidAmount?.toStringAsFixed(2)??' 0.00', unit: '¥')
                                  ],
                                ),
                              ),
                              _diverLine,
                              SizedBox(
                                height: XfgSize().size80,
                                child: Row(
                                  children: [
                                    _shopInfoItemWidget('店铺推广员', shop.salesAgent?.toString()?? '0'),
                                    _shopInfoItemWidget('店铺人数', shop.visitorCount?.toString()?? '0'),
                                    _shopInfoItemWidget('订单总数', shop.orderCount?.toString()?? '0')
                                  ],
                                ),
                              )
                            ],
                          );
                        }),
                      ),
                      /// 我的订单
                      Container(
                        decoration: white6Decoration,
                        margin: EdgeInsets.only(top: XfgSize().size10),
                        child: Column(
                          children: [
                            GestureDetector(
                              onTap: () => XfgNav.pushNamed(Routes.myShopOrder),
                              child: Container(
                                height: XfgSize().size56,
                                padding: EdgeInsets.symmetric(horizontal: XfgSize().size15),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    XfgText('我的订单', fontSize: XfgSize().text20, color: XfgColor.text1, bold: FontWeight.bold),
                                    const Spacer(),
                                    XfgText('全部   ', fontSize: XfgSize().text14, color: XfgColor.text3),
                                    Image.asset(Res.imagesArrowRight, width: XfgSize().size10, height: XfgSize().size15)
                                  ],
                                ),
                              ),
                            ),
                            _diverLine,
                            SizedBox(
                              height: XfgSize().size88,
                              child: Row(
                                children: [
                                  _orderItemWidget(Res.imagesShopToPay, '待付款', onTap: () => XfgNav.pushNamed(Routes.myShopOrder, arguments: OrderStatus.paying)),
                                  _orderItemWidget(Res.imagesShopDelivery, '待发货', onTap: () => XfgNav.pushNamed(Routes.myShopOrder, arguments: OrderStatus.sending)),
                                  _orderItemWidget(Res.imagesShopWaitingReceipt, '待收货', onTap: () => XfgNav.pushNamed(Routes.myShopOrder, arguments: OrderStatus.receiving)),
                                  _orderItemWidget(Res.imagesShopDone, '已完成', onTap: () => XfgNav.pushNamed(Routes.myShopOrder, arguments: OrderStatus.done))
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                      /// 我的服务
                      Container(
                        decoration: white6Decoration,
                        margin: EdgeInsets.only(top: XfgSize().size10),
                        child: Column(
                          children: [
                            Container(
                              height: XfgSize().size56,
                              padding: EdgeInsets.only(left: XfgSize().size15),
                              alignment: Alignment.centerLeft,
                              child: XfgText('我的服务', fontSize: XfgSize().text20, color: XfgColor.text1, bold: FontWeight.bold),
                            ),
                            _diverLine,
                            SizedBox(
                              height: XfgSize().size95,
                              child: Row(
                                children: [
                                  _serviceItemWidget(Res.imagesShopCommissionDetails, '提成明细', onTap: () => XfgNav.pushNamed(Routes.myShopCommissionDetails)),
                                  _serviceItemWidget(Res.imagesShopCustomerDetails, '客户明细', onTap: () => XfgNav.pushNamed(Routes.myShopCustomer)),
                                  _serviceItemWidget(Res.imagesShopPromoter, '推广员'),
                                  _serviceItemWidget(Res.imagesShopRules, '规则说明', onTap: () => XfgNav.pushNamed(Routes.rules, arguments: {'config': SystemConfig.storeRules}))
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
    );
  }

  /// 分割线
  Widget get _diverLine => Container(height: XfgSize().size0_5, color: XfgColor.colorF2);

  /// 店铺信息ItemWidget
  Widget _shopInfoItemWidget(String text, String value, {String unit = '', GestureTapCallback? onTap}) => Expanded(
    child: GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          XfgText(unit, fontSize: XfgSize().text12, color: XfgColor.text1D, children: [
            TextSpan(text: value, style: TextStyle(fontSize: XfgSize().text20, fontWeight: FontWeight.bold))
          ]),
          SizedBox(height: XfgSize().size15),
          XfgText(text, fontSize: XfgSize().text14, color: XfgColor.text2),
        ],
      ),
    ),
  );

  /// 订单ItemWidget
  Widget _orderItemWidget(String image, String text, {GestureTapCallback? onTap}) => Expanded(
    child: GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: onTap,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: [
          Image.asset(image, width: XfgSize().size28, height: XfgSize().size28),
          SizedBox(height: XfgSize().size10),
          XfgText(text, fontSize: XfgSize().text14, color: XfgColor.text2),
        ],
      ),
    ),
  );

  /// 服务ItemWidget
  Widget _serviceItemWidget(String image, String text, {GestureTapCallback? onTap}) => Expanded(
    child: InkWell(
      onTap: onTap,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: [
          Image.asset(image, width: XfgSize().size28, height: XfgSize().size22),
          SizedBox(height: XfgSize().size14),
          XfgText(text, fontSize: XfgSize().text14, color: XfgColor.text2),
        ],
      ),
    ),
  );
}
